layui.use(['form','layer','table','jquery'],function () {
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;

    var userId = localStorage.getItem("userId");
    var trueName = localStorage.getItem("trueName");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");

    //全部评论、正常、删除、已删除
    GoodsCountAllComment("",$("#span1"),3);
    GoodsCountAllComment("1",$("#span2"),3);
    GoodsCountAllComment("2",$("#span3"),3);
    GoodsCountAllComment("3",$("#span4"),3);
    function GoodsCountAllComment(status,name,type) {
        $.ajax({
            type:"post",
            dataType:"json",
            url:"/comment/GoodsCountAllComment",
            data:{"status":status,"type":type},
            success:function (data) {
                name.text(data.data);
                $("#in").val(1);
            }
        })
    };

    //管理人员全部评论、正常、删除、已删除
    function UserGoodsCountAllComment(status,name,type) {
        $.ajax({
            type:"post",
            dataType:"json",
            url:"/comment/UserGoodsCountAllComment",
            data:{"status":status,"type":type},
            success:function (data) {
                name.text(data.data);
                $("#in").val(2);
            }
        })
    };

    //表格数字数据
    selectNum("",3,"/comment/selectGoodsNumber");
    function selectNum(status,type,url){
        var commentDate = new Date();
        commentDate = getTime(commentDate);
        $.ajax({
            type:"post",
            dataType:"json",
            url:url,
            data:{"status":status,"type":type,"commentDate":commentDate},
            success:function (data) {
                var goodsCountAllComment = data.data.goodsCountAllComment;
                var countHistoricalCommentNum = data.data.countHistoricalCommentNum;
                var countCommentsToday = data.data.countCommentsToday;
                var countCommentPeople = data.data.countCommentPeople;


                var b1 = isRealNum(goodsCountAllComment);
                var b2 = isRealNum(countHistoricalCommentNum);
                var b3 = isRealNum(countCommentsToday);
                var b4 = isRealNum(countCommentPeople);

                $("#title").empty();
                $("#sum").empty();
                $("#title").append(
                    "<th>"+"历史评论次数"+"</th>" +
                    "<th>"+"历史评论人数"+"</th>" +
                    "<th>"+"今日评论次数"+"</th>" +
                    "<th>"+"今日评论人数"+"</th>"
                )
                $("#sum").append(
                    "<th>"+b1+"</th>"
                    +"<th>"+b2+"</th>"
                    +"<th>"+b3+"</th>"
                    +"<th>"+b4+"</th>"
                );
            }
        })
    };

    //表格详细数据
    var status = "";
    var type = 3;
    select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
    function select(status,type,url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"账号",align:"center"},
                {field:"nickname",title:"账户昵称",align:"center"},
                {field:"productName",title:"评论商品",align:"center"},
                {field:"createDate",title:"评论时间",align:"center"},
                {field:"content",title:"评论内容",align:"center"},
                {field:"status",title:"评论状态",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "正常";
                        }else if (data.status=="2") {
                            return "已拦截";
                        }else{
                            return "删除";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:lanjie("+data.id+");' style='color: #2F4056;margin-right: 10px;'>拦截</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                }}
            ]]
        })
    };
    function select2(status,type,url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"账号",align:"center"},
                {field:"nickname",title:"账户昵称",align:"center"},
                {field:"productName",title:"评论商品",align:"center"},
                {field:"createDate",title:"评论时间",align:"center"},
                {field:"content",title:"评论内容",align:"center"},
                {field:"status",title:"评论状态",align:"center"},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan2("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                    }}
            ]]
        })
    };
    //查看
    chakan = function chakan(id){
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id,"type":3},
            url:"/comment/selectGoodsComments",
            success:function (data) {
               chakanlayer(data.data,id);
            }
        });
    };
    function chakanlayer(data,id){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看商品评论",
            content:"/comment/comment-goods-see.html",
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                localStorage.setItem("commodityId",data.commodityId);
                localStorage.setItem("commentId",id);
                obj.find("#productName").text(data.productName);
                obj.find("#createDate").text(data.createDate);
                obj.find("#trueName").text(data.trueName);
                obj.find("#trueNamee").val(userName);
                obj.find("#currentpriceProduct").text(data.currentpriceProduct);
                obj.find("#searchKeywords").text(data.searchKeywords);
                obj.find("#title").text(data.title);
                if(data.recommend=="2"){
                    obj.find("#recommend").text('新品');
                }else{
                    obj.find("#recommend").text('推荐');
                }
                obj.find("#hits").text(data.hits);
                if(data.productSate=="0"){
                    obj.find("#productSate").text('已删除');
                }else if(data.productSate=="1"){
                    obj.find("#productSate").text('上架');
                }else{
                    obj.find("#productSate").text('下架');
                }
                obj.find("#mainImage").attr("src",'http://'+data.mainImage);
                obj.find("#productDescription").text(data.productDescription);
                obj.find("#account").val(account);
                obj.find("#userId").val(userId);
            }
        })
    };
    //拦截
    lanjie = function lanjie(id){
        layer.confirm("是否确定拦截这条评论",{
                icon:3,title:"提示"
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id,"status":2},
                    url:"/comment/delGoodsComment",
                    success:function (data) {
                        if(data){
                            layer.msg("删除成功");
                            location.reload();
                        }else{
                            layer.msg("删除失败");
                        }
                    }
                })
            }
        )
    }
    //删除
    shanchu = function shanchu(id){
       layer.confirm("是否确定删除这条评论",{
           icon:3,title:"提示"
       },
        function (index) {
            $.ajax({
                type:"post",
                dataType:"json",
                data:{"id":id,"status":3},
                url:"/comment/delGoodsComment",
                success:function (data) {
                    if(data){
                        layer.msg("删除成功");
                        location.reload();
                    }else{
                        layer.msg("删除失败");
                    }
                }
            })
        }
       )
    };
    //查看
    chakan2 = function chakan2(id){
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id,"type":3},
            url:"/comment/selectGoodsUserComments",
            success:function (data) {
                selectLayer(data.data,id);
            }
        })
    };
    function selectLayer(data,id){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看商品评论",
            content:"/comment/comment-goods-see2.html",
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                var productId = data.productId;
                var productId = localStorage.setItem('commodityId',data.commodityId);
                localStorage.setItem("commentId",id);
                obj.find("#productName").text(data.productName);
                obj.find("#createDate").text(data.createDate);
                obj.find("#trueName").text(data.trueName);
                obj.find("#trueNamee").val(userName);
                obj.find("#currentpriceProduct").text(data.currentpriceProduct);
                obj.find("#searchKeywords").text(data.searchKeywords);
                obj.find("#title").text(data.title);
                var recommend="";
                if(data.recommend=="2"){
                    recommend=="新品";
                }else{
                    recommend=="推荐";
                }
                obj.find("#recommend").text(recommend);
                obj.find("#hits").text(data.hits);
                obj.find("#productSate").text(data.productSate);
                obj.find("#mainImage").attr("src",'http://'+data.mainImage);
                obj.find("#productDescription").text(data.productDescription);
                obj.find("#account").val(account);
                obj.find("#userId").val(userId);
            }
        });
    };

    //全部评论
    $("#quanbu").click(function () {
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var value = $("#in").val();
        if(value=="1"){
            select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
            selectNum("",3,"/comment/selectGoodsNumber");
        }else if (value=="2") {
            select(status,type,"/comment/UserSelectGoodsComment?status="+status+"&type="+type);
            selectNum("",3,"/comment/UserSelectAll");
        }
    });
    //正常
    $("#zhengchang").click(function () {
        end("quanbu");
        show("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var status = 1;
        var type = 3;
        var value = $("#in").val();
        if(value=="1"){
            select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
            selectNum(1,3,"/comment/selectGoodsNumber");
        }else if (value=="2") {
            select2(status,type,"/comment/UserSelectGoodsComment?status="+status+"&type="+type);
            selectNum(1,3,"/comment/UserSelectAll");
        }
    });
    //已拦截
    $("#yilanjie").click(function () {
        end("zhengchang");
        end("quanbu");
        show("yilanjie");
        end("yishanchu");
        var status = 2;
        var type = 3;
        var value = $("#in").val();
        if(value=="1"){
            select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
            selectNum(2,3,"/comment/selectGoodsNumber");
        }else if(value=="2"){
            select2(status,type,"/comment/UserSelectGoodsComment?status="+status+"&type="+type);
            selectNum(2,3,"/comment/UserSelectAll");
        }
    });
    //已删除
    $("#yishanchu").click(function () {
        end("zhengchang");
        end("quanbu");
        end("yilanjie");
        show("yishanchu");
        var status = 3;
        var type = 3;
        var value = $("#in").val();
        if(value=="1"){
            select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
            selectNum(3,3,"/comment/selectGoodsNumber");
        }else if(value=="2"){
            select2(status,type,"/comment/UserSelectGoodsComment?status="+status+"&type="+type);
            selectNum(3,3,"/comment/UserSelectAll");
        }
    });
    //显示
    function show(elementId) {
        var obj1 = document.getElementById(elementId);
        obj1.style.backgroundColor="#2F4056";
        obj1.style.color="#FFFFFF";
    };
    //不显示
    function end(elementId) {
        var obj2 = document.getElementById(elementId);
        obj2.style.backgroundColor="#F9F9F9";
        obj2.style.color="#666666";
        obj2.style.border="1px solid #D2D2D2";
    };
    //前端用户
    $("#qianduan").click(function () {
        show("qianduan");
        end("guanli");
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var status="";
        var type=3;
        select(status,type,"/comment/selectGoodsComment?status="+status+"&type="+type);
        selectNum("",2,"/comment/selectGoodsNumber");
        GoodsCountAllComment("",$("#span1"),3);
        GoodsCountAllComment("1",$("#span2"),3);
        GoodsCountAllComment("2",$("#span3"),3);
        GoodsCountAllComment("3",$("#span4"),3);
    });
    //管理用户
    $("#guanli").click(function () {
       show("guanli");
       end("qianduan");
       show("quanbu");
       end("zhengchang");
       end("yilanjie");
       end("yishanchu");
       select2(status,type,"/comment/UserSelectGoodsComment?status="+status+"&type="+type);
       selectNum("",2,"/comment/UserSelectAll");
       UserGoodsCountAllComment("",$("#span1"),3);
       UserGoodsCountAllComment("1",$("#span2"),3);
       UserGoodsCountAllComment("2",$("#span3"),3);
       UserGoodsCountAllComment("3",$("#span4"),3);
    });
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today);
        return nowTime;
    };
    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

    function isRealNum(val){
        // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
        if(val === "" || val ==null){
            return 0;
        }
        if(!isNaN(val)){
            return val;
        }else{
            return 0;
        }
    }
    //刷新
    $('#shuaxin').click(function () {
       location.reload();
    });

    //返回
    $("#goBack").click(function (s) {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    });
});